package main

import (
	"encoding/xml"
	"errors"
	"fmt"
	"strings"
)

// ExtractInvokeResult 从给定的 XML 字符串中提取 <invokeResult> 标签之间的内容
func ExtractInvokeResult(xmlData string) (string, error) {
	// 查找起始标签位置
	startTag := "<invokeResult>"
	start := strings.Index(xmlData, startTag)
	if start == -1 {
		return "", errors.New("未找到起始标签 <invokeResult>")
	}
	start += len(startTag)

	// 查找结束标签位置
	endTag := "</invokeResult>"
	end := strings.Index(xmlData[start:], endTag)
	if end == -1 {
		return "", errors.New("未找到结束标签 </invokeResult>")
	}

	// 提取内容
	result := xmlData[start : start+end]

	return result, nil
}

// Envelope was generated 2025-04-01 07:29:40 by root on 3d8d9eeebe2458.
type Envelope struct {
	XMLName xml.Name `xml:"Envelope"`
	Text    string   `xml:",chardata"`
	Xsi     string   `xml:"xsi,attr"`
	Xsd     string   `xml:"xsd,attr"`
	Soapenv string   `xml:"soapenv,attr"`
	Scs     string   `xml:"scs,attr"`
	Header  struct {
		Text string `xml:",chardata"`
	} `xml:"Header"`
	Body struct {
		Text   string `xml:",chardata"`
		Invoke struct {
			Text          string `xml:",chardata"`
			EncodingStyle string `xml:"encodingStyle,attr"`
			XMLData       struct {
				Text    string `xml:",chardata"`
				Type    string `xml:"type,attr"`
				Soapenc string `xml:"soapenc,attr"`
			} `xml:"xmlData"`
		} `xml:"invoke"`
	} `xml:"Body"`
}

// Test1 test
func Test1() {

	data := `<?xml version="1.0" encoding="utf-8"?>
	<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
					 xmlns:xsd="http://www.w3.org/2001/XMLSchema"
					 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
		<soap:Body>
		  <invokeResponse xmlns="http://LSCService.chinamobile.com">
			<invokeResult>&lt;?xml version="1.0" encoding="UTF-8"?&gt;&lt;Response&gt;&lt;PK_Type&gt;&lt;Name&gt;LOGIN_ACK&lt;/Name&gt;&lt;/PK_Type&gt;&lt;Info&gt;&lt;Result&gt;1&lt;/Result&gt;&lt;FailureCause&gt;&lt;/FailureCause&gt;&lt;/Info&gt;&lt;/Response&gt;</invokeResult>
		  </invokeResponse>
		</soap:Body>`

	// 调用函数提取内容
	result, err := ExtractInvokeResult(data)
	if err != nil {
		fmt.Println("错误:", err)
		return
	}

	// 打印提取的内容
	fmt.Println("提取内容:", result)

}

// Test2 测试2
func Test2() {
	// 输入的SOAP报文内容
	data1 := `
<soapenv:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
                  xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
                  xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" 
                  xmlns:scs="http://SCService.chinatelecom.com">
   <soapenv:Header/>
   <soapenv:Body>
      <scs:invoke soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
         <xmlData xsi:type="soapenc:string" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">

		 
test

         </xmlData>
      </scs:invoke>
   </soapenv:Body>
</soapenv:Envelope>`

	// 解析XML到结构体
	var env Envelope
	if err := xml.Unmarshal([]byte(data1), &env); err != nil {
		panic(err)
	}

	// 输出提取的CDATA内容
	fmt.Println("Extracted XMLData Content:")
	fmt.Println(env.Body.Invoke.XMLData)
}

// 定义 XML 结构体
type DXEnvelope struct {
	XMLName xml.Name `xml:"http://schemas.xmlsoap.org/soap/envelope/ Envelope"`
	Body    DXBody   `xml:"http://schemas.xmlsoap.org/soap/envelope/ Body"`
}

type DXBody struct {
	Invoke DXInvoke `xml:"http://SCService.chinatelecom.com invoke"`
}

type DXInvoke struct {
	XMLData string `xml:"xmlData"`
}

// ExtractXMLData 从 SOAP 消息中提取 xmlData 内容
func ExtractXMLData(soapMessage string) (string, error) {

	var env DXEnvelope
	err := xml.Unmarshal([]byte(soapMessage), &env)
	if err != nil {
		return "", fmt.Errorf("解析 XML 出错: %v", err)
	}

	content := strings.TrimSpace(env.Body.Invoke.XMLData)
	return content, nil
}

func Test3() {
	// 示例 SOAP 消息
	soapMessage := `<soapenv:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
					  xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
					  xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" 
					  xmlns:scs="http://SCService.chinatelecom.com">
	   <soapenv:Header/>
	   <soapenv:Body>
		  <scs:invoke soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
			 <xmlData xsi:type="soapenc:string" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">
	
			 
                  <![CDATA[


<?xml version="1.0" encoding="UTF-8"?>
<Request>
	<PK_Type>
		<Name>SET_FACTORYCONFIG</Name>
		<Code>307</Code>
	</PK_Type>
	<Info>
		<SUID>00-35-9a-0c-ee-2f</SUID>
		<Values>
			<FileList>
				<ConfigFile FileName="factoryconfig1.xml" />
				<ConfigFile FileName="factoryconfig2.xml" />
			</FileList>
		</Values>
	</Info>
</Request>



             ]]>
	
			 </xmlData>
		  </scs:invoke>
	   </soapenv:Body>
	</soapenv:Envelope>`

	// 调用函数提取 xmlData
	content, err := ExtractXMLData(soapMessage)
	if err != nil {
		fmt.Println("错误:", err)
		return
	}

	fmt.Printf("提取到的 xmlData 内容: [%s]\n", content)
}

func main() {
	Test3()
}
